//
//  main.c
//  hashtable
//
//  Created by chenyh on 2021/1/26.
//

#include <stdio.h>
#include <stdlib.h>
#include "hashdb.h"
typedef struct _student {
    char          name[10];     //姓名
    char          sex;          //性别
    float         height,weight;//身高,体重
}student_t;
int db_id_to_idx(unsigned char id[6]) //通过id得到数组索引
{
    int i;
    int sum = 0;
    for (i = 0; i < 6; i++) {
        sum += id[0];
    }
    return  sum % 250;
}
//随机产生一条学生记录
int student_info_generate(unsigned char *p_id,student_t *p_student)
{
    int i;
    for (i = 0; i < 6; i++) {
        p_id[i] = rand();
    }
    for (i = 0; i < 9; i++) {
        p_student->name[i] = (rand()%('z'-'a')) + 'a';
    }
    p_student->name[i] = '\0'; //字符串结束符;
    p_student->sex     = (rand() & 0x01) ? 'F':'M'; //随机性别
    p_student->height  = (float)rand()/rand();
    p_student->weight = (float)rand()/rand();
    return 0;
    
}
int main(int argc, const char * argv[]) {
    student_t stu;
    unsigned char id[6];
    int i;
    hash_db_t hash_students;
    hash_db_init(&hash_students, 250, 6, sizeof(student_t), (hash_func_t)db_id_to_idx);
    
    for (i = 0; i < 100; i++) { //添加100个学生的信息
        //设置学生的信息，当前一随机数当做测试
        student_info_generate(id, &stu);
        if (hash_db_search(&hash_students, id, &stu) == 0) {
            printf("该ID的记录已经存在！\n");
            continue;
        }
        printf("ID:%02x%02x%02x%02x%02x%02x\n",id[0],id[1],id[2],id[3],id[4],id[5]);
        printf("%s%c%.2f%.2f\n",stu.name,stu.sex,stu.height,stu.weight);
     
        if(hash_db_add(&hash_students,id,&stu) != 0) {
            printf("添加失败！\n");
        }

    }
    
    printf("ID:%02x%02x%02x%02x%02x%02x\n",id[0],id[1],id[2],id[3],id[4],id[5]);
    
    if (hash_db_search(&hash_students, id, &stu) == 0) {
        printf("%s%c%.2f%.2f\n",stu.name,stu.sex,stu.height,stu.weight);
    }else {
        printf("未找到该ID的记录!\r\n");
    }
    hash_db_deinit(&hash_students);
    return 0;
}
